﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

using BarcodeLib;
using MM.Entities;
using MM.Business;

using System.IO;

namespace MM
{
    public partial class frmInNhan : Form
    {
        public BarcodeLib.Barcode bar = new BarcodeLib.Barcode();
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";

        public List<VAT_TU_PHU_TUNG> lstVTPT = new List<VAT_TU_PHU_TUNG>();
        List<THONG_TIN_NHAN> lstNhan = new List<THONG_TIN_NHAN>();
        
        frmTimKiemVTPT frm;
        frmTimKiemTheoHinh frm1;
        frmImpa_TimKiem frm2;
        frmImpa_TimKiemTheoHinh frm3;
        string[] arrThamSo = new string[10];
        
        public frmInNhan(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;

            _matau = frmDangNhap.objTau._maTau;
            this.InitDMKho(_matau);
            this.InitTrangThai();

            this.InitBarCode();
            this.ResetForm();
            this.InitThamSo();
        }

        public void InitThamSo()
        {
            THAM_SO objTS = new THAM_SO();
            clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
            objTS = tsCtrl.SelectTHAM_SO("vThuocTinh");
            if (objTS != null && objTS.GiaTri != "")
            {
                arrThamSo = objTS.GiaTri.Split(',');
            }
        }

        public void InitBarCode()
        {
            bar.BackColor = Color.White;
            bar.ForeColor = Color.Black;            
            bar.IncludeLabel = false;
            bar.Width = 1000;
            bar.Height=200;
            bar.Alignment = AlignmentPositions.LEFT;                   
            
        }

        private void InitDMKho(string matau)
        {
            clsKHO khoCtrl = new clsKHO(_sConn);
            List<KHO> lstKho = new List<KHO>();
            lstKho = khoCtrl.LoadKHOByTAU(matau);
            this.lookUpKho.Properties.DataSource = lstKho;
            this.lookUpKho.Properties.DisplayMember = "TenKho";
            this.lookUpKho.Properties.ValueMember = "MaKho";

            this.lookupDMKho.DataSource = lstKho;
            this.lookupDMKho.DisplayMember = "TenKho";
            this.lookupDMKho.ValueMember = "MaKho";
           
        }

        private void InitTrangThai()
        {
            clsTRANG_THAI_VTPT ttCtrl = new clsTRANG_THAI_VTPT(_sConn);
            List<TRANG_THAI_VTPT> lst = new List<TRANG_THAI_VTPT>();
            lst = ttCtrl.usp_SelectTRANG_THAI_VTPTsAll();            
            this.lookUpTrangthai.Properties.DataSource = lst;
            this.lookUpTrangthai.Properties.DisplayMember = "TenTrangThai";
            this.lookUpTrangthai.Properties.ValueMember = "MaTrangThai";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // insert hoặc update phieu in nhan
            clsPHIEU_IN_NHAN pinCtrl = new clsPHIEU_IN_NHAN(_sConn);
            List<THONG_TIN_NHAN> lstNhan1 = new List<THONG_TIN_NHAN>();

            THONG_TIN_NHAN obj;
            for (int i = 0; i < lstNhan.Count; i++)
            {
                if (lstNhan[i].SoLuongIn > 0 && lstNhan[i].MaKho != "" && lstNhan[i].MaKho != null)
                {
                    obj = new THONG_TIN_NHAN();
                    obj = lstNhan[i];
                    obj.NguoiTao = _nguoitao;
                    obj.NgayTao = DateTime.Today;
                    lstNhan1.Add(obj);
                }
            }

            if (lstNhan1.Count > 0 )
            {
                int kq=-1;
                if (optSoPO.Checked == true)
                    kq = pinCtrl.usp_InsertPHIEU_IN_NHAN(lstNhan1);
                else
                    kq = 1;
                if (kq >= 1)
                {
                    ReportDocument rptLabel = new Label();

                    List<THONG_TIN_NHAN> lstLable = new List<THONG_TIN_NHAN>();

                    int pos = 0;
                    try
                    {
                        pos = int.Parse(this.txtPos.Text.Trim()) - 1;
                    }
                    catch { }


                    for (int i = 0; i < pos; i++)
                    {
                        obj = new THONG_TIN_NHAN();
                        lstLable.Add(obj);
                    }

                    for (int i = 0; i < lstNhan1.Count; i++)
                    {
                        obj = new THONG_TIN_NHAN();
                        obj.MaVTPT = lstNhan1[i].MaVTPT;
                        obj.TenVTPT = lstNhan1[i].TenVTPT;
                        obj.MaTrangThai = lstNhan1[i].MaTrangThai;
                        obj.TenTrangThai = lstNhan1[i].TenTrangThai;
                        obj.TenNSX = lstNhan1[i].TenNSX;
                        obj.MaKho = lstNhan1[i].MaKho;
                        // Tạo mã vạch
                        bar.Encode(TYPE.CODE128, obj.MaVTPT.Trim() + "#" + obj.MaKho.Trim() + "#" + obj.MaTrangThai.ToString());                       
                        obj.BarCode = bar.GetImageData(SaveTypes.BMP);
                        // end of Tạo mã vạch

                        for (int j = 0; j < lstNhan1[i].SoLuongIn; j++)
                        {
                            lstLable.Add(obj);
                        }
                    }


                    rptLabel.SetDataSource(lstLable);

                    crystalReportViewer1.ReportSource = rptLabel;
                }
            }
            else
            {
                if (lstNhan1.Count == 0)
                    MessageBox.Show("VUI LÒNG CHỌN KHO ĐỂ IN NHÃN");
                else
                    MessageBox.Show("TẠO PHIẾU IN KHÔNG THÀNH CÔNG");
            }
        }

        private void txtSoPhieuPO_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    lstNhan.Clear();

                    if (this.txtSoPhieuPO.Text.Trim().Length == 10)
                    {
                        this.HienthiCHI_TIET_PDH(this.txtSoPhieuPO.Text.Trim());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void HienthiCHI_TIET_PDH(string POThamChieuSAP)
        {
            THONG_TIN_NHAN obj;
            List<CHI_TIET_PDH> lstpdh = new List<CHI_TIET_PDH>();
            clsCHI_TIET_PDH ctpdhCtrl = new clsCHI_TIET_PDH(_sConn);
            lstpdh = ctpdhCtrl.usp_SelectCHI_TIET_PDHByPOThamChieuSAP(POThamChieuSAP);
            lstpdh.RemoveAll(delegate(CHI_TIET_PDH x) { return (x.TinhTrang == 1); });

            clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
            List<THUOC_TINH> lstTT = new List<THUOC_TINH>();
            clsNHA_SAN_XUAT nsxCtrl = new clsNHA_SAN_XUAT(_sConn);

            for (int i = 0; i < lstpdh.Count; i++)
            {
                if (lstpdh[i].AccAssignment.Trim() == "")
                {
                    obj = new THONG_TIN_NHAN();
                    obj.MaVTPT = lstpdh[i].MaThietBi;
                    obj.TenVTPT = lstpdh[i].TenVTPT;
                    obj.MaTrangThai = lstpdh[i].MaTrangThai;                    
                    obj.SoLuong = lstpdh[i].SoLuong;
                    obj.SoLuongIn = (int)Math.Round(double.Parse(obj.SoLuong.ToString()), 0);
                    obj.TenTrangThai = lstpdh[i].TenTrangThai;
                    obj.MaKho = lstpdh[i].oThongTinNhan.MaKho;
                    obj.MaPhieuIn = lstpdh[i].oThongTinNhan.MaPhieuIn;
                    obj.POThamChieuSAP = POThamChieuSAP;
                    obj.MaChiTietPDH = lstpdh[i].MaChiTiet;

                    lstTT.Clear();
                    lstTT = ttCtrl.SelectTHUOC_TINHByID(obj.MaVTPT);
                    foreach (THUOC_TINH t in lstTT)
                    {
                        if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                        {
                            if (t._giaTriTT != "" && t._giaTriTT != null)
                            {
                                obj.TenVTPT += ";" + t._giaTriTT;
                            }
                        }
                    }

                    obj.TenNSX = nsxCtrl.SelectNHA_SAN_XUATByID(lstpdh[i].MaThietBi,_matau).tenNhaSanXuat;

                    lstNhan.Add(obj);
                }

            }

            this.gridControl1.DataSource = lstNhan;
            this.gridControl1.RefreshDataSource();
        }

        private void HienthiCHI_TIET_PNH(string MaPhieuNhapHang)
        {
            try
            {
                lstNhan.Clear();

                if (MaPhieuNhapHang.Length == 10)
                {
                    clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
                    List<THUOC_TINH> lstTT = new List<THUOC_TINH>();
                    THONG_TIN_NHAN obj;
                    clsCHI_TIET_PNH ctpnhCtrl = new clsCHI_TIET_PNH(_sConn);
                    clsNHA_SAN_XUAT nsxCtrl = new clsNHA_SAN_XUAT(_sConn);
                    List<CHI_TIET_PNH> lst = new List<CHI_TIET_PNH>();
                    lst = ctpnhCtrl.usp_SelectCHI_TIET_PNH_ByMaPhieuNhapHang(this.txtSoPhieuGR.Text.Trim());
                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (lst[i].AccAssignment.Trim() == "")
                        {
                            obj = new THONG_TIN_NHAN();
                            obj.MaVTPT = lst[i].MaVTPT;
                            obj.TenVTPT = lst[i].TenVTPT;
                            obj.MaTrangThai = lst[i].MaTrangThai;
                            obj.SoLuong = lst[i].SoLuong;
                            obj.SoLuongIn = (int)Math.Round(double.Parse(obj.SoLuong.ToString()), 0);
                            obj.TenTrangThai = lst[i].TenTrangThai;
                            obj.MaKho = lst[i].MaKho;
                            // obj.MaPhieuIn = lst[i].MaPhieuIn;
                            obj.TenTrangThai = lst[i].TenTrangThai;
                            obj.POThamChieuSAP = lst[i].POThamChieuSAP;
                            obj.MaChiTietPDH = lst[i].MaChiTietPDH;

                            lstTT.Clear();
                            lstTT = ttCtrl.SelectTHUOC_TINHByID(obj.MaVTPT);
                            foreach (THUOC_TINH t in lstTT)
                            {
                                if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                {
                                    if (t._giaTriTT != "" && t._giaTriTT != null)
                                    {
                                        obj.TenVTPT += ";" + t._giaTriTT;
                                    }
                                }

                            }

                            obj.TenNSX = nsxCtrl.SelectNHA_SAN_XUATByID(lst[i].MaVTPT,_matau).tenNhaSanXuat;

                            lstNhan.Add(obj);
                        }
                    }
                    this.gridControl1.DataSource = lstNhan;
                    this.gridControl1.RefreshDataSource();
                }
            }
            catch { }
        }

        private void HienthiCHI_TIET_PCK(string MaPhieuCK)
        {
            lstNhan.Clear();
            clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
            List<THUOC_TINH> lstTT = new List<THUOC_TINH>();
            clsNHA_SAN_XUAT nsxCtrl = new clsNHA_SAN_XUAT(_sConn);
            clsCHI_TIET_PCK ctpxhCtrl = new clsCHI_TIET_PCK(_sConn);
            THONG_TIN_NHAN obj;
            List<CHI_TIET_PCK> lst = new List<CHI_TIET_PCK>();
            lst = ctpxhCtrl.usp_SelectCHI_TIET_PCK_ByMaPhieuChuyenKho(MaPhieuCK);
            lst.RemoveAll(delegate(CHI_TIET_PCK x) { return x.TinhTrang == 1; });
            for (int i = 0; i < lst.Count; i++)
            {
                obj = new THONG_TIN_NHAN();
                obj.MaVTPT = lst[i].MaThietBi;
                obj.TenVTPT = lst[i].TenVTPT;
                obj.MaTrangThai = lst[i].MaTrangThai;
                obj.SoLuong = lst[i].SoLuong;
                obj.SoLuongIn = (int)Math.Round(double.Parse(obj.SoLuong.ToString()), 0);
                obj.TenTrangThai = lst[i].TenTrangThai;
                obj.MaKho = lst[i].MaKhoDich;

                lstTT.Clear();
                lstTT = ttCtrl.SelectTHUOC_TINHByID(obj.MaVTPT);
                foreach (THUOC_TINH t in lstTT)
                {
                    if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                    {
                        if (t._giaTriTT != "" && t._giaTriTT != null)
                        {
                            obj.TenVTPT += ";" + t._giaTriTT;
                        }
                    }
                }

                obj.TenNSX = nsxCtrl.SelectNHA_SAN_XUATByID(lst[i].MaThietBi,_matau).tenNhaSanXuat;

                lstNhan.Add(obj);
            }
            this.gridControl1.DataSource = lstNhan;
            this.gridControl1.RefreshDataSource();
        }

        private void lnkSoPO_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.optSoPO.Checked = true;
            frmTimPhieuDH f = new frmTimPhieuDH(_sConn);
            f.chonPDH += new frmTimPhieuDH.ChonPhieuDatHang(GetPhieuDatHang);
            f.Show(this);
        }

        private void GetPhieuDatHang(object sender, ChonPhieuDHEventArgs e)
        {
            lstNhan.Clear();
            this.HienthiCHI_TIET_PDH(e.POThamChieuSAP);
            this.txtSoPhieuPO.Text = e.POThamChieuSAP;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string makho = this.lookUpKho.GetColumnValue("MaKho").ToString();
                int matrangthai = int.Parse(this.lookUpTrangthai.GetColumnValue("MaTrangThai").ToString());
                string tentrangthai = this.lookUpTrangthai.GetColumnValue("TenTrangThai").ToString();
                if (lstVTPT.Count > 0 || this.txtMaVTPT.Text.Trim() != "")
                {
                    if (lstVTPT.Count == 0)
                    {
                        VAT_TU_PHU_TUNG obj = new VAT_TU_PHU_TUNG();

                        clsVAT_TU_PHU_TUNG vtptCtrl = new clsVAT_TU_PHU_TUNG(_sConn);
                        obj = vtptCtrl.SelectVAT_TU_PHU_TUNG(this.txtMaVTPT.Text.Trim(),frmDangNhap.objTau.maTau);
                        if (obj != null && obj._maVTPT != "" && obj._maVTPT!=null )
                        {                          

                            lstVTPT.Add(obj);
                        }
                        else
                        {
                            MessageBox.Show("KHÔNG TỒN TẠI VTPT NÀY:"+this.txtMaVTPT.Text);
                            return;
                        }
                    }
                    if (makho != "" && matrangthai > -1)
                    {
                        THONG_TIN_NHAN oTTN;
                        clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
                        clsNHA_SAN_XUAT nsxCtrl = new clsNHA_SAN_XUAT(_sConn);
                        List<THUOC_TINH> lstTT = new List<THUOC_TINH>();

                        for (int i = 0; i < lstVTPT.Count; i++)
                        {
                            oTTN = new THONG_TIN_NHAN();
                            oTTN.MaVTPT = lstVTPT[i]._maVTPT;
                            oTTN.TenVTPT = lstVTPT[i]._tenVTPT;
                            oTTN.MaTrangThai = matrangthai;
                            oTTN.TenTrangThai = tentrangthai;
                            oTTN.SoLuongIn = 1;
                            oTTN.POThamChieuSAP = "";
                            oTTN.MaChiTietPDH = 0;
                            oTTN.MaPhieuIn = 0;
                            oTTN.MaKho = makho;

                            lstTT.Clear();
                            lstTT = ttCtrl.SelectTHUOC_TINHByID(oTTN.MaVTPT);
                            foreach (THUOC_TINH t in lstTT)
                            {
                                if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                {
                                    if (t._giaTriTT != "" && t._giaTriTT != null)
                                    {
                                        oTTN.TenVTPT += ";" + t._giaTriTT;
                                    }
                                }
                            }

                            oTTN.TenNSX = lstVTPT[i]._tenNSX;

                            lstNhan.Add(oTTN);                            
                        }
                        this.gridControl1.DataSource = lstNhan;
                        this.gridControl1.RefreshDataSource();
                        lstVTPT.Clear();
                    }
                }
            }
            catch { MessageBox.Show("Vui lòng chọn đầy đủ thông tin"); }
        }

        private void cOPYToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int index = -1;
                index = gridDSVT.FocusedRowHandle;
                if (index >= 0 && optSoGR.Checked==false)
                {
                    THONG_TIN_NHAN oTTN = new THONG_TIN_NHAN();
                    oTTN.MaVTPT = lstNhan[index].MaVTPT;
                    oTTN.TenVTPT = lstNhan[index].TenVTPT;
                    oTTN.MaTrangThai = lstNhan[index].MaTrangThai;
                    oTTN.TenTrangThai = lstNhan[index].TenTrangThai;
                    oTTN.SoLuong = lstNhan[index].SoLuong;
                    oTTN.POThamChieuSAP = lstNhan[index].POThamChieuSAP;
                    oTTN.MaChiTietPDH = lstNhan[index].MaChiTietPDH;
                    oTTN.MaPhieuIn = 0;
                    oTTN.MaKho = "";

                    lstNhan.Add(oTTN);

                    this.gridControl1.RefreshDataSource();
                }
                else
                    MessageBox.Show("Vui lòng chọn dòng dữ liệu cần Sao chép để In nhãn");

            }
            catch { }
        }

        private void frmInNhan_Paint(object sender, PaintEventArgs e)
        {
            //Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void txtSoPhieuGR_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                this.HienthiCHI_TIET_PNH(this.txtSoPhieuGR.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.optSoGR.Checked = true;
            frmTimPhieuNK f = new frmTimPhieuNK(_sConn);
            f.chonPNK += new frmTimPhieuNK.ChonPhieuNhapKho(GetPhieuNhapKho);
            f.Show(this);

        }

        private void GetPhieuNhapKho(object sender, ChonPhieuNKEventArgs e)
        {
            string mapnk = e.MaPhieuNhapKho;
            this.txtSoPhieuGR.Text = mapnk;
            this.HienthiCHI_TIET_PNH(mapnk);            
        }

        private void optSoGR_CheckedChanged(object sender, EventArgs e)
        {
            this.ResetForm();
        }

        private void lookUpKho_QueryPopUp(object sender, CancelEventArgs e)
        {
            this.optKho.Checked = true;
        }

       

        private void tìmTheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (frm == null)
            {
                frm = new frmTimKiemVTPT(_sConn,true);
                frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count >= 1)
                {
                    this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;
                    
                }
               
            }
        }

        private void tìmTheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm1 == null)
            {
                frm1 = new frmTimKiemTheoHinh(_sConn,true);
                frm1.SelectLstMaterialByImg += new frmTimKiemTheoHinh.SelectListMaterialByImgHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm1.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count >= 1)
                {
                    this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;
                    
                }
                
            }
        }

        private void LuuDSVTPT(object sender, lstVtptEventArgs e)
        {
            lstVTPT = e.lstVtpt;
            if (lstVTPT.Count == 1)
            {
                this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;               
            }
        }

        private void frmInNhan_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    lstNhan.Clear();

                    if (this.txtSoPhieuCK.Text.Trim().Length == 10)
                    {
                        this.HienthiCHI_TIET_PCK(this.txtSoPhieuCK.Text.Trim());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void linkLabel2_Click(object sender, EventArgs e)
        {
            this.optCK.Checked = true;
            frmTimPhieuCK f = new frmTimPhieuCK(_sConn);
            f.chonPCK += new frmTimPhieuCK.ChonPhieuChuyenKho(GetPhieuChuyenKho);
            f.Show(this);
        }
        private void GetPhieuChuyenKho(object sender, ChonPhieuCKEventArgs e)
        {
            string mapck = e.MaPhieuChuyenKho;
            this.txtSoPhieuCK.Text = mapck;
            this.HienthiCHI_TIET_PCK(mapck);            
        }

        private void ResetForm()
        {
            if (optSoPO.Checked == true)
            {
                this.txtSoPhieuPO.Enabled = true;
                this.txtSoPhieuGR.Enabled = false;
                this.txtSoPhieuCK.Enabled = false;
                this.txtSoPhieuGR.Text = "";
                this.txtSoPhieuCK.Text = "";
                this.lookUpKho.Enabled = false;

                this.contextMenuStrip1.Enabled = true;
            }
            else
                if (optSoGR.Checked == true)
                {
                    this.txtSoPhieuGR.Enabled = true;
                    this.txtSoPhieuPO.Enabled = false;
                    this.txtSoPhieuCK.Enabled = false;
                    this.txtSoPhieuPO.Text = "";
                    this.txtSoPhieuCK.Text = "";
                    this.lookUpKho.Enabled = false;

                    this.contextMenuStrip1.Enabled = false;
                }
                else
                    if (optCK.Checked == true)
                    {
                        this.txtSoPhieuCK.Enabled = true;
                        this.txtSoPhieuGR.Enabled = false;
                        this.txtSoPhieuPO.Enabled = false;
                        this.txtSoPhieuGR.Text = "";
                        this.txtSoPhieuPO.Text = "";
                        this.lookUpKho.Enabled = false;

                        this.contextMenuStrip1.Enabled = false;
                    }
                    else
                        if (optKho.Checked == true)
                        {
                            this.txtSoPhieuCK.Enabled = false;
                            this.txtSoPhieuGR.Enabled = false;
                            this.txtSoPhieuPO.Enabled = false;
                            this.txtSoPhieuGR.Text = "";
                            this.txtSoPhieuPO.Text = "";
                            this.txtSoPhieuCK.Text = "";
                            this.lookUpKho.Enabled = true;
                            this.contextMenuStrip1.Enabled = true;
                        }
        }

        private void optSoPO_CheckedChanged(object sender, EventArgs e)
        {
            this.ResetForm();
        }

        private void optCK_CheckedChanged(object sender, EventArgs e)
        {
            this.ResetForm();
        }

        private void optKho_CheckedChanged(object sender, EventArgs e)
        {
            this.ResetForm();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.lstNhan.Clear();
            this.gridControl1.RefreshDataSource();
        }

        private void tìmVTIMPATheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
            {
                frm2 = new frmImpa_TimKiem(_sConn, true);
                frm2.SelectLstMaterial += new frmImpa_TimKiem.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm2.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count >= 1)
                {
                    this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;

                }

            }
        }

        private void tìmVTIMPATheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm3 == null)
            {
                frm3 = new frmImpa_TimKiemTheoHinh(_sConn, true);
                frm3.SelectLstMaterial += new frmImpa_TimKiemTheoHinh.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm3.ShowDialog();
            if (lstVTPT.Count > 0)
            {
                if (lstVTPT.Count >= 1)
                {
                    this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;

                }

            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton1.Checked == true)
            {
                this.cmdBrowse.Enabled = true;
            }
            else
                this.cmdBrowse.Enabled = false;
        }

        private void cmdBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog f = new OpenFileDialog();
                f.Filter = "Text file (*.txt*)|*.txt*";
                f.Title = "CHỌN FILE ĐỂ IN NHÃN: MAVTPT <TAB> MATRANGTHAI <TAB> MAKHO <TAB> SOLUONGIN";
                if (f.ShowDialog() == DialogResult.OK)
                {                  
                    this.DocNoiDungFile(f.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DocNoiDungFile(string filename)
        {
            try
            {
                VAT_TU_PHU_TUNG obj = new VAT_TU_PHU_TUNG();
                clsVAT_TU_PHU_TUNG vtptCtrl = new clsVAT_TU_PHU_TUNG(_sConn);

                THONG_TIN_NHAN oTTN;
                clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
                List<THUOC_TINH> lstTT = new List<THUOC_TINH>();
                clsTRANG_THAI_VTPT trangthaiCtrl = new clsTRANG_THAI_VTPT(_sConn);
                clsKHO khoCtrl = new clsKHO(_sConn);
                KHO objKho=new KHO();

                StreamReader reader = new StreamReader(File.Open(filename, FileMode.Open));
                while (!reader.EndOfStream)
                {
                    try
                    {
                        string line = reader.ReadLine();
                        string[] arrvalue = new string[10];
                        arrvalue = line.Split('\t');

                        obj = new VAT_TU_PHU_TUNG();
                        obj = vtptCtrl.SelectVAT_TU_PHU_TUNG(arrvalue[0], frmDangNhap.objTau.maTau);
                        if (obj != null && obj._maVTPT != "" && obj._maVTPT != null)
                        {
                            oTTN = new THONG_TIN_NHAN();
                            oTTN.MaVTPT = obj._maVTPT;
                            oTTN.TenVTPT = obj._tenVTPT;
                            oTTN.MaTrangThai = int.Parse(arrvalue[1]);
                            oTTN.TenNSX = obj._tenNSX;
                            oTTN.TenTrangThai = (trangthaiCtrl.usp_SelectTRANG_THAI_VTPTByMaTrangThai(oTTN.MaTrangThai)).TenTrangThai;

                            if (oTTN.TenTrangThai != "")
                            {

                                try
                                {
                                    oTTN.SoLuongIn = int.Parse(arrvalue[3]);
                                }
                                catch { oTTN.SoLuongIn = 1; }
                                oTTN.POThamChieuSAP = "";
                                oTTN.MaChiTietPDH = 0;
                                oTTN.MaPhieuIn = 0;
                                oTTN.MaKho = arrvalue[2];

                                objKho = khoCtrl.usp_SelectKHO_ByMaKho(oTTN.MaKho);
                                if (objKho != null)
                                {
                                    lstTT.Clear();
                                    lstTT = ttCtrl.SelectTHUOC_TINHByID(oTTN.MaVTPT);
                                    foreach (THUOC_TINH t in lstTT)
                                    {
                                        if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                        {
                                            if (t._giaTriTT != "" && t._giaTriTT != null)
                                            {
                                                oTTN.TenVTPT += ";" + t._giaTriTT;
                                            }
                                        }
                                    }
                                    lstNhan.Add(oTTN);
                                }
                                
                            }
                        }
                    }
                    catch { reader.Close(); }
                }
                this.gridControl1.DataSource = lstNhan;
                this.gridControl1.RefreshDataSource();
                reader.Close();
                reader.Dispose();
            }
            catch { }
        }
    }
}